(function(mui, window) {
	/*
	 * callback(status,data/errormessage)
	 * status 0(成功) 1(失败) 2(网络错误)
	 */

	var userNameDigest = 'security_oasis_super';
	var passwordDigest = 'h3c_security_oasis_super';

	var waitingViewCount = 0;
	var baseRequest = window.baseRequest = {};
	var commonTool = window.commonTool;
	var dataManager = window.dataManager;

	// 超时时间默认
	var timeoutDef = 5000;

	function showWaitingView() {
		//console.log('开始请求数据：' + $.util.formatDate(new Date(), 'y-M-d H:m:s'));
		if(waitingViewCount === 0) {
			commonTool.showWaiting();
		}
		waitingViewCount++;
	}

	function closeWaitingView() {
		//console.log('请求成功：' + $.util.formatDate(new Date(), 'y-M-d H:m:s'));
		waitingViewCount--;
		if(waitingViewCount === 0) {
			//setTimeout(function () {
			commonTool.closeWaiting();
			//}, 200);
		}
	}

	var _ajax = mui.ajax;

	/**
	 * 响应的code  0 -> 成功   1 -> 失败   2 -> 网络错误
	 * @param options
	 * @param callback  回调函数，非必须的   callback(code,data,xhr)
	 * @returns {xhr}
	 */
	mui.ajax = function(options, callback) {
		return _ajax(mui.extend(true, options, {
			beforeSend: showWaitingView, // 请求之前添加等待窗
			complete: closeWaitingView, //  请求完成删除等待窗
			dataType: 'json', //  默认处理成JSON
			//crossDomain:true,
			success: function(data, status, xhr) {
//				console.log("xhr1:" + xhr.responseText);
				if(status === 'success') {
					callback && callback(0, data, xhr);
				} else {
					console.log("xhr2:" + xhr.status);
					callback && callback(1, data, xhr);
				}
			},
			error: function(xhr, type) {
				console.log("xhr3:" + xhr.status + "type:" + type);
				console.log("xhr1:" + xhr.responseText);
				if(xhr.status === 0) {
					if(type === 'timeout') {
						//  没有缓存dataManager.getLocale()是因为在加载这个js的时候，语言还没有初始化
						commonTool.toast(dataManager.getLocale().base.err.timeout);
					} else {
						commonTool.toast(dataManager.getLocale().base.err.network);
					}
				}
				callback && callback(2, type, xhr);
			}
		}));
	};

	/**
	 *
	 * @param url
	 * @param timeout  可选
	 * @param callback
	 * @returns {xhr}
	 */
	baseRequest.commonGet = function(url, timeout, callback) {
		if(timeout instanceof Function) {
			callback = timeout;
			timeout = timeoutDef;
		}
		return mui.ajax({
			url: url,
			type: 'get',
			timeout: timeout
		}, callback);
	};

	/**
	 *
	 * @param url
	 * @param postData 必须传一个，哪怕{}也好
	 * @param timeout  可选
	 * @param callback
	 * @returns {xhr}
	 */
	baseRequest.commonPost = function(url, postData, timeout, callback) {
		if(timeout instanceof Function) {
			callback = timeout;
			timeout = timeoutDef;
		}
		return mui.ajax({
			url: url,
			type: 'post',
			timeout: timeout,
			data: postData,
			contentType: 'application/json; charset=utf-8'
		}, callback);
	};
	/**
	 *
	 * @param url
	 * @param timeout  可选
	 * @param callback
	 * @returns {xhr}
	 */
	baseRequest.digestGet = function(url, timeout, callback) {
		if(timeout instanceof Function) {
			callback = timeout;
			timeout = timeoutDef;
		}
		return mui.ajax({
			url: url,
			type: 'get',
			timeout: timeout,
			username: userNameDigest,
			password: passwordDigest
		}, callback);
	};
	/**
	 * 使用认证发post请求
	 * @param url
	 * @param postData  如果没有参数，一定要传一个 {} 否则会出错
	 * @param timeout
	 * @param callback
	 * @returns {xhr}
	 */
	baseRequest.digestPost = function(url, postData, timeout, callback) {
		if(timeout instanceof Function) {
			callback = timeout;
			timeout = timeoutDef;
		}
		return mui.ajax({
			url: url,
			type: 'post',
			timeout: timeout,
			username: userNameDigest,
			password: passwordDigest,
			data: postData,
			contentType: 'application/json; charset=utf-8'
		}, callback);
	};

	baseRequest.digestDelete = function(url, timeout, callback) {
		if(timeout instanceof Function) {
			callback = timeout;
			timeout = timeoutDef;
		}
		return mui.ajax({
			url: url,
			type: 'delete',
			contentType: 'application/json; charset=utf-8',
			timeout: timeout,
			username: userNameDigest,
			password: passwordDigest
		}, callback);
	}
	
	baseRequest.commonDelete = function(url, timeout, callback) {
		if(timeout instanceof Function) {
			callback = timeout;
			timeout = timeoutDef;
		}
		return mui.ajax({
			url: url,
			type: 'delete',
			contentType: 'application/json; charset=utf-8',
			timeout: timeout
		}, callback);
	}
	
	baseRequest.getStaticFile = function(url, callback){
		return mui.ajax({
			url: url,
			type: 'get',
			headers: {
					'Content-Type': 'application/json'
				},
		}, callback);
		
		/*return mui.ajax({
				url: url,
				type: 'get',
				headers: {
					'Content-Type': 'application/json'
				},
				success: callback
			});*/
	}
}(mui, window));